package dao

import (
    "database/sql"
    "fmt"
    _ "github.com/go-sql-driver/mysql"

)

type DaoClient struct {
    db          *sql.DB
    sqlsvr      string
    maxidel     int
    maxactive   int
}

func NewDaoClient(sqlsvr string, maxidel int, maxactive int) *DaoClient {
    dc := new(DaoClient)
    if dc == nil {
        return nil
    }
    db, _ := sql.Open("mysql", "root:123456@tcp(" + sqlsvr + ")/shark?charset=utf8")
    if db == nil {
        return nil
    }
    db.SetMaxOpenConns(2000)
    db.SetMaxIdleConns(1000)
    db.Ping()
    dc.db = db
    dc.sqlsvr = sqlsvr
    dc.maxidel = maxidel
    maxactive = maxactive

    return dc
}

func (dc *DaoClient) InsertUser(user_id int, user_name string){
    db := dc.db
    stmt, err := db.Prepare("insert into user(user_id,user_name)values(?,?)")
    CheckErr(err)
    if result, err := stmt.Exec(user_id,user_name); err == nil {
        if id, err := result.LastInsertId(); err == nil {
            fmt.Println("insert id : ",id)
        }
    }
}

func (dc *DaoClient) Insertproduct(product_id int, product_num int){
    db := dc.db
    stmt, err := db.Prepare("insert into product(product_id,product_num)values(?,?)")
    CheckErr(err)
    if result, err := stmt.Exec(product_id, product_num); err == nil {
        if id, err := result.LastInsertId(); err == nil {
            fmt.Println("insert id : ",id)
        }
    }
}

func (dc *DaoClient) Insertorder(user_id int, product_id int, goods_id int, status int){
    db := dc.db
    stmt, err := db.Prepare("insert into orderInfo(user_id,product_id,goods_id,status)values(?,?,?,?)")
    CheckErr(err)
    if result, err := stmt.Exec(user_id, product_id, goods_id, status); err == nil {
      if id, err := result.LastInsertId(); err==nil {
          fmt.Println("insert id : ",id)
      }
    }
}

//查询用户秒杀接口
func (dc *DaoClient) QueryUser(userid int ,product_id int) int{
    db := dc.db
    rows, err := db.Query("select * from orderInfo where user_id = ? and product_id = ?", userid, product_id)
    defer rows.Close()
    CheckErr(err)
    var res int
    if false == rows.Next() {
        return -1
    } else {
        var id int
        var order_id int
        var user_id int
        var product_id int
        var goods_id int
        var status int
        err = rows.Scan(&id, &order_id, &user_id, &product_id, &goods_id, &status)
        CheckErr(err)
        res = goods_id
    }
    return res
}

//查询商品秒杀接口
func (dc *DaoClient) QueryProduct(productid int)  []map[string]int{
    db := dc.db
    rows, err := db.Query("select * from orderInfo where product_id= ? ",productid);
    defer rows.Close()
    CheckErr(err)

    var prolist []map[string] int
    prolist = make([]map[string]int, 0)

    var index int
    var hs map[string] int

    for rows.Next() {
        hs = make(map[string] int)
        var id int
        var order_id int
        var user_id int
        var product_id int
        var goods_id int
        var status int
        err = rows.Scan(&id, &order_id, &user_id, &product_id, &goods_id, &status)
        CheckErr(err)

        hs["goodsid"] = goods_id
        hs["userid"] = user_id
        prolist = append(prolist, hs)
        index = index + 1
    }
    return prolist
}

func CheckErr(err error) {
   if err != nil {
       panic(err)
   }
}
